package org.zachary.creditbusiness.commodityandhouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.zachary.creditbusiness.hotel.entity.Ban;
import org.zachary.creditbusiness.hotel.mapper.BanMapper;
import org.zachary.creditbusiness.commodityandhouse.mapper.HouseOrderMapper;
import org.zachary.creditbusiness.common.service.UnifiedBanService;

import java.util.List;

@RestController
@RequestMapping("/houses")
public class HouseBanController {

    @Autowired
    private BanMapper banMapper;

    @Autowired
    private HouseOrderMapper houseOrderMapper;

    @Autowired
    private UnifiedBanService unifiedBanService;

    /**
     * 检查用户是否被封禁
     * @param userId 用户ID
     * @return 1表示被封禁，0表示未被封禁
     */
    @GetMapping("/checkban")
    public int checkBan(@RequestParam Long userId) {
        int cnt = Math.toIntExact(banMapper.selectCount(
                new QueryWrapper<Ban>().eq("user_id", userId)
        ));
        return cnt > 0 ? 1 : 0;
    }

    /**
     * 恢复用户：删除 creditbusiness_ban 表中所有该 userId 的记录
     * 但只有当用户支付完所有房屋、酒店和商品订单后才能恢复
     * @param userId 用户ID
     * @return 恢复结果
     */
    @PostMapping("/recover")
    public ResponseEntity<String> recoverUser(@RequestParam Long userId) {
        try {
            // 使用统一的封禁服务检查恢复条件
            UnifiedBanService.BanRecoveryResult result = unifiedBanService.checkRecoveryEligibility(userId);

            if (!result.isCanRecover()) {
                return ResponseEntity.badRequest().body(
                        "用户 " + userId + " 不符合恢复条件: " + String.join(", ", result.getReasons())
                );
            }

            // 执行恢复
            int deleted = unifiedBanService.recoverUser(userId);
            return ResponseEntity.ok("恢复用户 " + userId + "，共删除 " + deleted + " 条封禁记录");

        } catch (Exception e) {
            return ResponseEntity.badRequest().body(
                    "恢复用户 " + userId + " 失败: " + e.getMessage()
            );
        }
    }

    /**
     * 获取用户的所有封禁记录
     * @param userId 用户ID
     * @return 封禁记录列表
     */
    @GetMapping("/bans")
    public List<Ban> getUserBans(@RequestParam Long userId) {
        QueryWrapper<Ban> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        return banMapper.selectList(query);
    }

    /**
     * 检查用户恢复资格
     * @param userId 用户ID
     * @return 恢复资格详情
     */
    @GetMapping("/recovery-eligibility")
    public UnifiedBanService.BanRecoveryResult checkRecoveryEligibility(@RequestParam Long userId) {
        return unifiedBanService.checkRecoveryEligibility(userId);
    }

    /**
     * 根据UUID删除特定的封禁记录
     * @param uuid 订单UUID
     * @return 删除结果
     */
    @DeleteMapping("/ban/{uuid}")
    public ResponseEntity<String> deleteBanByUuid(@PathVariable String uuid) {
        int deleted = banMapper.delete(
                new QueryWrapper<Ban>().eq("uuid", uuid)
        );
        return ResponseEntity.ok("删除UUID " + uuid + " 的封禁记录，共删除 "
                + deleted + " 条记录");
    }
}